/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ddd.service;

import com.ddd.context.Parameters;
import com.ddd.entity.Journal;
import com.ddd.util.HibernateUtil;
import com.ddd.util.Util;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

public class JournalService extends BaseService {

    private Parameters para;
    private LogService logService;

    public JournalService(HttpServletRequest request) {
        para = new Parameters(request);
        logService = new LogService(request);
    }

    public Journal load(String id) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();

        Journal journal = (Journal) session.load(Journal.class, id);
        //Hibernate.initialize(journal);

        tx.commit();
        session.close();

        return journal;
    }

    public ArrayList loadAll() {
        ArrayList result = new ArrayList();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Journal.class);
        Iterator iter = criteria.list().iterator();
        while (iter.hasNext()) {
            result.add(iter.next());
        }
        tx.commit();
        session.close();
        para.setTotalSize(result.size());
        return result;
    }

    public void save(Journal journal) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        journal.setCid(para.getUsername());
        journal.setCtime(Util.getTodayWithTime());
        session.save(journal);
        tx.commit();
        session.close();
    }

    public void update(Journal journal) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        journal.setUid(para.getUsername());
        journal.setUtime(Util.getTodayWithTime());
        session.update(journal);
        tx.commit();
        session.close();
    }

    public void delete(Journal journal) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        session.delete(journal);
        tx.commit();
        session.close();
    }

    public ArrayList search(String name, String foundYear, String publisherID) {
        ArrayList result = new ArrayList();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Journal.class);

        if (Util.isNotBlank(name)) {
            criteria.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
        }

        if (Util.isNotBlank(foundYear)) {
            criteria.add(Restrictions.ilike("foundYear", foundYear, MatchMode.ANYWHERE));
        }

        // 搜尋限制
        if (Util.isNotBlank(publisherID)) {
           criteria.add(Restrictions.eq("fkPublisherId", publisherID));
        }

        Iterator iter = criteria.list().iterator();
        while (iter.hasNext()) {
            result.add(iter.next());
        }
        tx.commit();
        session.close();
        return result;
    }

    public ArrayList loadByPublisher(String publihserID) {
        ArrayList result = new ArrayList();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Journal.class);

        if (Util.isNotBlank(publihserID)) {
            logger.debug("publihserID = " + publihserID);
            criteria.add(Restrictions.eq("fkPublisherId", publihserID));
        }

        Iterator iter = criteria.list().iterator();
        while (iter.hasNext()) {
            result.add(iter.next());
        }
        tx.commit();
        session.close();
        logger.debug("result size = " + result.size());
        return result;
    }
}
